using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace JufGame
{
	/// <summary>
	/// 多层感知机
	/// </summary>
	public class MLP
	{
		public float[] outputData;
		public BaseLayer[] hiddenLayers;
		private ComputeBuffer inputeBuffer;
		public MLP(WeightBiasMemory memory, int batch)
		{
			//全连接层-激活函数层交替，知道最后一个全连接层
			hiddenLayers = new BaseLayer[memory.WeiBiasArray.Length * 2 - 1];
			for(int i = 0; i < hiddenLayers.Length; ++i)
			{
				var curWB = memory.WeiBiasArray[i / 2];
				if((i & 1) == 0)
				{
					hiddenLayers[i] = new Affine(memory.affine, batch, curWB.inputCount, curWB.outputCount, curWB.weights, curWB.bias);
				}
				else
				{
					hiddenLayers[i] = new ActivateFunc(memory.activateFunc, batch, curWB.outputCount, memory.isLoadLastOutput);
				}
			}
			inputeBuffer = new ComputeBuffer(batch * memory.WeiBiasArray[0].inputCount, sizeof(float));
			outputData = new float[batch * memory.WeiBiasArray[^1].outputCount];
		}

		public void Predict(float[] input)
		{
			inputeBuffer.SetData(input);
			var curInput = inputeBuffer;
			foreach(var l in hiddenLayers)
			{
				l.Forward(curInput);
				curInput = l.outputBuffer;
			}
			curInput.GetData(outputData);
		}

		public void Release()
		{
			foreach(var h in hiddenLayers)
			{
				h.Release();
			}
			inputeBuffer.Release();
		}
	}
}
